package com.baoai.service;


import com.baoai.dto.APIResponse;
import com.baoai.dto.SearchIndexDto;
import com.baoai.util.LuenceSearchUtil;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * 索引service层
 * Created by xiaren on 2017/3/3.
 */
public class SearchService {

    public static Logger logger = Logger.getLogger(SearchService.class);

    /**
     * 查询接口
     * @param searchText 搜索文本
     * @param type 类型 类型【1：视频、2问答、3课程、4文章、5专家】
     * @param pageIndex 页数
     * @param pageSize 行数
     * @return
     */
    public APIResponse<Object> searchIndex(String searchText, String type, int pageIndex, int pageSize) {
        logger.info("调用查询索引接口:searchText=" + searchText + ",type=" + type + ",pageIndex=" + pageIndex + ",pageSize=" + pageSize);
        APIResponse<Object> apiResponse = new APIResponse<Object>();
        try{
            String[] searchArray = LuenceSearchUtil.ikSeqWord(searchText);
            List<SearchIndexDto> list = LuenceSearchUtil.complexearchIndex(searchArray, type, pageIndex, pageSize);
            apiResponse.setData(list);
        } catch (Exception e){
            e.printStackTrace();
            apiResponse.setMessage("索引查询失败!" + e.getMessage());
            apiResponse.setStatus(APIResponse.FAIL);
            logger.error("索引查询失败!", e);
        }
        return apiResponse;
    }

    /**
     * 删除索引
     * @param id ID
     * @param type 类型 类型【1：视频、2问答、3课程、4文章、5专家】
     * @return
     */
    public APIResponse deleteIndex(String id, String type) {
        logger.info("调用删除接口:id=" + id + ",type=" + type);
        APIResponse apiResponse = new APIResponse();
        try{
            LuenceSearchUtil.deleteSearchIndex(id, type);
        } catch (Exception e){
            e.printStackTrace();
            apiResponse.setMessage("索引删除失败!" + e.getMessage());
            apiResponse.setStatus(APIResponse.FAIL);
            logger.error("索引删除失败!", e);
        }
        return apiResponse;
    }

    /**
     * 添加或删除索引（有则更新无则添加）
     * @param searchIndexDto
     * @return
     */
    public APIResponse saveOrUpdateIndex(SearchIndexDto searchIndexDto) {
        logger.info("调用更新接口:searchIndexDto=" + searchIndexDto.toString());
        APIResponse apiResponse = new APIResponse();
        try{
            LuenceSearchUtil.saveOrUpdateIndex(searchIndexDto);
        } catch (Exception e){
            e.printStackTrace();
            apiResponse.setMessage("索引更新失败!" + e.getMessage());
            apiResponse.setStatus(APIResponse.FAIL);
            logger.error("索引更新失败!", e);
        }
        return apiResponse;
    }

    /**
     * 查询接口
     * @param type 类型 类型【1：视频、2问答、3课程、4文章、5专家】
     * @return
     */
    public APIResponse<Object> searchAllIndex(String type) {
        logger.info("调用查询所有索引接口:type=" + type);
        APIResponse<Object> apiResponse = new APIResponse<Object>();
        try{
            List<SearchIndexDto> list = LuenceSearchUtil.searchAllIndex(type);
            apiResponse.setData(list);
        } catch (Exception e){
            e.printStackTrace();
            apiResponse.setMessage("索引查询失败!" + e.getMessage());
            apiResponse.setStatus(APIResponse.FAIL);
            logger.error("索引查询失败!", e);
        }
        return apiResponse;
    }




}
